package com.imooc.api.intercept;

import com.google.gson.Gson;
import com.imooc.base.BaseInfoProperties;
import com.imooc.pojo.Admin;
import com.imooc.pojo.Users;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

/**
 * @author 啊昌
 * @date 2025/9/12 23:59
 * JWTCurrentUserInterceptor JWT拦截器
 */
@Component
@Slf4j
public class JWTCurrentUserInterceptor extends BaseInfoProperties implements HandlerInterceptor {

	public static ThreadLocal<Users> currentUser = new ThreadLocal<>();
	public static ThreadLocal<Admin> adminUser = new ThreadLocal<>();

	/**
	 * 在请求处理之前进行拦截处理
	 * 该方法主要用于从请求头中获取用户信息并存储到ThreadLocal中，供后续业务处理使用
	 *
	 * @param request  HTTP请求对象，用于获取请求头信息
	 * @param response HTTP响应对象
	 * @param handler  处理器对象
	 * @return boolean 返回true表示继续执行后续拦截器和目标方法，返回false表示中断执行
	 * @throws Exception 处理过程中可能抛出的异常
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		// 使用ThreadLocal可以在同一个线程内共享数据
		// 比如：Interceptor --> 控制器Controller --> 业务层Service --> 数据层Mapper(dao)
		// 从request拿到token 请求
		String appUserJson = request.getHeader(APP_USER_JSON);
		String saasUserJson = request.getHeader(SAAS_USER_JSON);
		String adminUserJson = request.getHeader(ADMIN_USER_JSON);


		if (StringUtils.isNotBlank(appUserJson)) {
			// 处理管理员用户信息，将其存储到ThreadLocal中
			appUserJson = URLDecoder.decode(appUserJson, StandardCharsets.UTF_8.toString());
			Users appUser = new Gson().fromJson(appUserJson, Users.class);
			currentUser.set(appUser);
		}

		if (StringUtils.isNotBlank(saasUserJson)) {
			// 处理管理员用户信息，将其存储到ThreadLocal中
			saasUserJson = URLDecoder.decode(saasUserJson, StandardCharsets.UTF_8.toString());
			Users appUser = new Gson().fromJson(saasUserJson, Users.class);
			currentUser.set(appUser);
		}

//		if (StringUtils.isNotBlank(appUserJson)
//				|| StringUtils.isNotBlank(saasUserJson)) {
//			// 处理普通用户信息，将其存储到ThreadLocal中
//			appUserJson = URLDecoder.decode(saasUserJson, StandardCharsets.UTF_8.toString());
//			Users appUser = new Gson().fromJson(appUserJson, Users.class);
//			currentUser.set(appUser);
//		}

		if (StringUtils.isNotBlank(adminUserJson)) {
			// 处理管理员用户信息，将其存储到ThreadLocal中
			adminUserJson = URLDecoder.decode(adminUserJson, StandardCharsets.UTF_8.toString());
			Admin admin = new Gson().fromJson(adminUserJson, Admin.class);
			adminUser.set(admin);
		}
		return true;
	}


	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
	}

	/**
	 * 请求处理完成后的回调方法，在请求处理完毕后执行清理工作
	 *
	 * @param request  HTTP请求对象
	 * @param response HTTP响应对象
	 * @param handler  处理请求的处理器对象
	 * @param ex       请求处理过程中抛出的异常，如果没有异常则为null
	 * @throws Exception 如果清理过程中发生错误
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		//清除线程的数据
		currentUser.remove();
		adminUser.remove();
	}

}
